package com.dss.service;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

import com.dss.dao.ExpertClassDAO;
import com.dss.dao.ExpertDAO;
import com.dss.dao.ExpertDocDAO;
import com.dss.po.Expert;
import com.dss.po.ExpertClass;
import com.dss.po.ExpertDoc;
import com.infomanage.exception.DaoException;
import com.infomanage.util.Consts;
import com.infomanage.util.FileUpload;
import com.infomanage.util.StringUtil;
import com.infomanage.util.smartupload.SmartRequest;
import com.infomanage.util.smartupload.SmartUpload;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.webtree.util.StringUtils;

public class ExpertService {

	public static Expert loadExpertInfoById(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("utf-8");
			String itemId = request.getParameter("expertId");
			if (StringUtils.isNotEmpty(itemId)) {
				ExpertDAO dao = new ExpertDAO();
				return dao.find(Expert.class, Integer.parseInt(itemId));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ExpertDoc loadDocById(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("utf-8");
			String itemId = request.getParameter("expertDocId");
			if (StringUtils.isNotEmpty(itemId)) {
				ExpertDocDAO dao = new ExpertDocDAO();
				return dao.find(ExpertDoc.class, Integer.parseInt(itemId));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<Expert> loadExpertInfoByExpertClass(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("utf-8");
			String classId = request.getParameter("expertClassId");
			if (StringUtils.isNotEmpty(classId)) {
				ExpertDAO dao = new ExpertDAO();
				return dao.findExpertsByClassId(Integer.parseInt(classId));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<ExpertDoc> loadDocsByExpertId(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("utf-8");
			String expertId = request.getParameter("expertId");
			if (StringUtils.isNotEmpty(expertId)) {
				ExpertDocDAO dao = new ExpertDocDAO();
				return dao.findDocsByExpertId(Integer.parseInt(expertId));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<ExpertClass> loadExpertClasses(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("utf-8");
			ExpertClassDAO dao = new ExpertClassDAO();
			return dao.findAll(ExpertClass.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ExpertClass loadExpertClassById(HttpServletRequest request, HttpServletResponse response,
			JspWriter out){
		try {
			request.setCharacterEncoding("utf-8");
			String expertClassId = request.getParameter("expertClassId");
			if(StringUtils.isNotEmpty(expertClassId)){
				ExpertClassDAO dao = new ExpertClassDAO();
				return dao.find(ExpertClass.class, Integer.parseInt(expertClassId));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject modifyExpertClass(HttpServletRequest request, HttpServletResponse response,
			JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("utf-8");
			ExpertClassDAO dao = new ExpertClassDAO();
			ExpertClass expertClass = dao.find(ExpertClass.class, Integer.parseInt(request.getParameter("expertClassId")));
			if(expertClass != null){
				expertClass.setTitle(request.getParameter("className"));
			}
			dao.modify(expertClass);
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
		
	}
	
	public static ReturnObject saveExpertClass(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("utf-8");
			String className = request.getParameter("className");
			if (StringUtils.isNotEmpty(className)) {
				ExpertClassDAO dao = new ExpertClassDAO();
				ExpertClass expertClass = new ExpertClass();
				expertClass.setTitle(className);
				dao.add(expertClass);
			}
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
			e.printStackTrace();
		}
		return ret;
	}

	public static ReturnObject removeExpertClass(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("utf-8");
			String classId = request.getParameter("classId");
			if (StringUtils.isNotEmpty(classId)) {
				ExpertDAO expertDAO = new ExpertDAO();
				List<Expert> list = expertDAO.findExpertsByClassId(Integer.parseInt(classId));
				if (list != null && list.size() > 0) {
					ret.SetErrored(true);
					ret.setErrorMessage("当前分类下存在专家信息，不能删除！");
					return ret;
				}
				ExpertClassDAO dao = new ExpertClassDAO();
				dao.remove(ExpertClass.class, Integer.parseInt(classId));
			}
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
			e.printStackTrace();
		}
		return ret;
	}

	public static ReturnObject operateExpertInfo(HttpServletRequest httpRequest, PageContext pageContext) {
		ReturnObject retObj = null;
		try {
			httpRequest.setCharacterEncoding("UTF-8");
			String actionType = httpRequest.getParameter("actionType");
			if (StringUtils.isEmpty(actionType)) {
				FileUpload fileUpload = new FileUpload("png,jpg,gif");
				SmartUpload smartUpload = fileUpload.upload(httpRequest,
						pageContext);
				SmartRequest request = smartUpload.getRequest();
				actionType = request.getParameter("actionType");
				String	expertClassId = request.getParameter("expertClassId");
				String expertId = request.getParameter("expertId");
				System.out.println(expertId);
				if ("add".equals(actionType)) {
					retObj = saveExpertInfo(smartUpload, expertClassId);
				}
				if ("modify".equals(actionType)) {
					retObj = modifyExpertInfo(smartUpload);
				}
			}else if("del".equals(actionType)){
				removeExpertInfo(httpRequest);
			}
		} catch (Exception e) {
			e.printStackTrace();
			retObj.setErrorMessage(e.getMessage());
		}
		return retObj;
	}

	
	public static ReturnObject operateExpertDoc(HttpServletRequest httpRequest, PageContext pageContext) {
		ReturnObject retObj = null;
		try {
			httpRequest.setCharacterEncoding("UTF-8");
			String actionType = httpRequest.getParameter("actionType");
			if (StringUtils.isEmpty(actionType)) {
				FileUpload fileUpload = new FileUpload("doc,docx");
				SmartUpload smartUpload = fileUpload.upload(httpRequest,
						pageContext);
				SmartRequest request = smartUpload.getRequest();
				actionType = request.getParameter("actionType");
				String	expertId = request.getParameter("expertId");
				if ("add".equals(actionType)) {
					retObj = saveExpertDoc(smartUpload, expertId);
				}
				if ("modify".equals(actionType)) {
					retObj = modifyExpertDoc(smartUpload);
				}
			}else if("del".equals(actionType)){
				removeExpertDoc(httpRequest, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			retObj.setErrorMessage(e.getMessage());
		}
		return retObj;
	}
	
	private static ReturnObject saveExpertInfo(SmartUpload smartUpload, String expertClassId) {
		SmartRequest request = smartUpload.getRequest();
		String[] fileNames = smartUpload.getFileNames();
		ReturnObject ret = new ReturnObject();
		try {
			Expert expert = new Expert();
			if (fileNames != null && fileNames.length > 0) {
				String dir = fileNames[0].substring(fileNames[0].lastIndexOf("\\") + 1);
				expert.setPhoto(dir);
			}
			expert.setName(request.getParameter("name"));
			expert.setExpertClassId(Integer.parseInt(request.getParameter("expertClassId")));
			expert.setProfessionalTitle(request.getParameter("professionalTitle"));
			expert.setDescription(request.getParameter("description"));
			ExpertDAO dao = new ExpertDAO();
			dao.add(expert);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
			e.printStackTrace();
		}
		return ret;
	}
	
	private static ReturnObject modifyExpertInfo(SmartUpload smartUpload){
		SmartRequest request = smartUpload.getRequest();
		String[] fileNames = smartUpload.getFileNames();
		String expertId = request.getParameter("expertId");
		ReturnObject ret = new ReturnObject();
		try {
			ExpertDAO dao = new ExpertDAO();
			Expert expert = dao.find(Expert.class, Integer.parseInt(expertId));
			if(expert != null){
				if (fileNames != null && fileNames.length > 0) {
					String dir = fileNames[0].substring(fileNames[0].lastIndexOf("\\") + 1);
					expert.setPhoto(dir);
				}
				expert.setName(request.getParameter("name"));
				expert.setExpertClassId(Integer.parseInt(request.getParameter("expertClassId")));
				expert.setProfessionalTitle(request.getParameter("professionalTitle"));
				expert.setDescription(request.getParameter("description"));
				dao.modify(expert);
			}
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
			e.printStackTrace();
		}
		return ret;
	}
	
	private static void removeExpertInfo(HttpServletRequest request) throws UnsupportedEncodingException, NumberFormatException, DaoException {
		request.setCharacterEncoding("UTF-8");
		String expertId = request.getParameter("expertId");
		ExpertDAO dao = new ExpertDAO();
		dao.removeExpert(Integer.parseInt(expertId));
	}

	private static ReturnObject saveExpertDoc(SmartUpload smartUpload, String expertId) {
		SmartRequest request = smartUpload.getRequest();
		String[] fileNames = smartUpload.getFileNames();
		ReturnObject ret = new ReturnObject();
		try {
			ExpertDoc doc = new ExpertDoc();
			if (fileNames != null && fileNames.length > 0) {
				String docName = fileNames[0].substring(fileNames[0].lastIndexOf("\\") + 1);
				doc.setStoreName(docName);
			}
			doc.setExpertId(Integer.parseInt(expertId));
			doc.setName(request.getParameter("name"));
			doc.setKeywords(request.getParameter("keywords"));
			doc.setSummary(request.getParameter("summary"));
			ExpertDocDAO dao = new ExpertDocDAO();
			dao.add(doc);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
			e.printStackTrace();
		}
		return ret;
	}
	
	public static ReturnObject removeExpertDoc(HttpServletRequest request, String expertDocId){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			if(StringUtils.isEmpty(expertDocId)){
				expertDocId = request.getParameter("expertDocId");
			}
			ExpertDocDAO dao = new ExpertDocDAO();
			ExpertDoc doc = dao.find(ExpertDoc.class, Integer.parseInt(expertDocId));
			String storeName = doc.getStoreName();
			String path = request.getRealPath(Consts.FILE_UPLOAD_SAVE_DIR);
			dao.remove(ExpertDoc.class, Integer.parseInt(expertDocId));
			File file = new File(path + "/" + storeName);
			file.delete();
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	private static ReturnObject modifyExpertDoc(SmartUpload smartUpload){
		SmartRequest request = smartUpload.getRequest();
		String expertDocId = request.getParameter("expertDocId");
		ReturnObject ret = new ReturnObject();
		try {
			ExpertDocDAO dao = new ExpertDocDAO();
			ExpertDoc expertDoc = dao.find(ExpertDoc.class, Integer.parseInt(expertDocId));
			if(expertDoc != null){
				expertDoc.setName(request.getParameter("name"));
				expertDoc.setKeywords(request.getParameter("keywords"));
				expertDoc.setSummary(request.getParameter("summary"));
				dao.modify(expertDoc);
			}
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
			e.printStackTrace();
		}
		return ret;
	}
}
